package xml;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXDemo {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		/**
		 * SAX解析器
		 */
		//解析器工厂
		SAXParserFactory spf=SAXParserFactory.newInstance();
		//获取解析器对象
		SAXParser sp=spf.newSAXParser();
		//执行解析
		sp.parse(new InputSource("newer.xml"),new MyHandler());
		
	}
}

//定义一个内部类作为自定义处理器
	class MyHandler extends DefaultHandler{
		
		//文档开始
		@Override
		public void startDocument() throws SAXException {
			System.out.println("文档开始");
		}
		
		//节点开始
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes)
				throws SAXException {
			System.out.println("节点开始 -->节点名称:"+qName);
		}
		
		//内容
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			System.out.println("内容-->"+new String(ch,start,length));
		}
		
		//节点结束
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			System.out.println("节点结束");
		}
		
		//文档结束
		@Override
		public void endDocument() throws SAXException {
			System.out.println("文档结束");
		}
	}
	


